package cn.shengchao.wecom.handler;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.shengchao.examstar.rpc.wecom.dto.WeComDeptUserDto;
import cn.shengchao.wecom.utils.JsonUtils;
import cn.shengchao.wecom.wxcp.acl.ports.repositories.ICpCallbackMsgRepository;
import cn.shengchao.wecom.wxcp.acl.ports.repositories.ICpExternalInviteRepository;
import cn.shengchao.wecom.wxcp.acl.ports.repositories.ICpUserRepository;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.constant.WxCpConsts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@Slf4j
@Component
public class LogHandler extends AbstractHandler {

    @Autowired
    ICpUserRepository cpUserRepository;
    @Autowired
    ICpExternalInviteRepository cpExternalInviteRepository;
    @Autowired
    ICpCallbackMsgRepository cpCallbackMsgRepository;
    @Override
    public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService cpService,
                                    WxSessionManager sessionManager) {
        log.info("\n接收到请求消息，内容：{}", JsonUtils.toJson(wxMessage));
        cpCallbackMsgRepository.save(wxMessage);
        if (wxMessage.getMsgType().equals(WxConsts.XmlMsgType.EVENT)&& wxMessage.getEvent().equals(WxCpConsts.EventType.CHANGE_EXTERNAL_CONTACT)){
            if (WxCpConsts.ExternalContactChangeType.ADD_EXTERNAL_CONTACT.equals(wxMessage.getChangeType())){
                // 指客户和成员关系建立，即客户同意企业发送的消息
                cpExternalInviteRepository.createExternalInvite(wxMessage.getUserId(), wxMessage.getExternalUserId(), wxMessage.getState());
            } else if (WxCpConsts.ExternalContactChangeType.DEL_EXTERNAL_CONTACT.equals(wxMessage.getChangeType())) {
                 // 指客户删除成员
                cpExternalInviteRepository.deleteExternalInvite(wxMessage.getUserId(), wxMessage.getExternalUserId());
            }else if (WxCpConsts.ExternalContactChangeType.EDIT_EXTERNAL_CONTACT.equals(wxMessage.getChangeType())) {

            }else if (WxCpConsts.ExternalContactChangeType.ADD_HALF_EXTERNAL_CONTACT.equals(wxMessage.getChangeType())) {
                // 半添加,指客户加成员，成员未同意或者拒绝

            }else if (WxCpConsts.ExternalContactChangeType.DEL_FOLLOW_USER.equals(wxMessage.getChangeType())) {
                // 指客户删除成员
                cpExternalInviteRepository.deleteExternalInvite(wxMessage.getUserId(), wxMessage.getExternalUserId());
            }


        }else if (wxMessage.getMsgType().equals(WxConsts.XmlMsgType.EVENT)&& wxMessage.getEvent().equals(WxCpConsts.EventType.CHANGE_CONTACT)){
            if (WxCpConsts.ContactChangeType.CREATE_USER.equals(wxMessage.getChangeType())){
                WeComDeptUserDto user = new WeComDeptUserDto();
                user.setUserId(wxMessage.getUserId());
                user.setDepartment(wxMessage.getMainDepartment().toString());
                user.setGender(wxMessage.getGender());
                user.setName(wxMessage.getName());
                user.setEmail(wxMessage.getEmail());
                user.setBizEmail(wxMessage.getBizMail());
                user.setMobile(wxMessage.getMobile());
                user.setIsLeaderInDept(JSONUtil.toJsonStr(wxMessage.getIsLeaderInDept()));
                cpUserRepository.messageSaveUser(user);

            }else if (WxCpConsts.ContactChangeType.UPDATE_USER.equals(wxMessage.getChangeType())){
                if (StrUtil.isNotBlank(wxMessage.getNewUserId())){
                    cpUserRepository.updateUserNewId(wxMessage.getUserId(),wxMessage.getNewUserId());
                }
            }else if (WxCpConsts.ContactChangeType.DELETE_USER.equals(wxMessage.getChangeType())){
                cpUserRepository.deleteUser(wxMessage.getUserId());
            }
        }
        return null;
    }

}
